﻿using FreeSql.DataAnnotations;
using Mapster;

namespace FindLatestFile.Models;

[Table(Name = "fm_file_info")]
public class FileInfoEx : BindableBase
{
    public string Name { get; set; } = default!;
    public string FullName { get; set; } = default!;
    public string Extension { get; set; } = default!;
    public long Length { get; set; }
    public DateTime CreationTime { get; set; }
    public DateTime LastWriteTime { get; set; }
    public string HashCode { get; set; } = default!;
    public bool IsIgnored { get; set; }
    public FileState State { get; set; }

    public FileInfoEx()
    {
        State = FileState.待处理;
    }

    public FileInfoEx(FileInfo info)
    {
        info.Adapt(this);
        State = FileState.待处理;
    }

    public FileInfoEx(string fileName) : this(new FileInfo(fileName))
    {
    }

    public bool IsSimilar(FileInfoEx file)
    {
        if (file.Length != Length)
            return false;
        if (file.CreationTime.Subtract(CreationTime).TotalSeconds > 1)
            return false;
        if (file.LastWriteTime.Subtract(LastWriteTime).TotalSeconds > 1)
            return false;

        return true;
    }

    public static FileInfoEx GetFileInfo(FileInfo file)
    {
        var info = new FileInfoEx(file);
        info.HashCode = HashHelper.GetSHA256FromFile(info.FullName);

        return info;
    }

    public static FileInfoEx GetFileInfo(string fileName)
    {
        return GetFileInfo(new FileInfo(fileName));
    }
}

public enum FileState
{
    标志,
    待处理,
    已处理,
}

public enum OrderType
{
    默认排序,
    文件名,
    文件大小,
    扩展名,
    创建时间,
    修改时间,
    哈希值,
    完整路径
}
